﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CHPM.BL.Manager_sInterfaces;
using CHPM.Entities;
using CHPM.IC.ObjectMapper;

namespace CHPM.BL.Managers
{
    public class PatientManager : IPatientManager
    {
        #region Fields

        private readonly IObjectMapper _objectMapper;

        #endregion

        #region Implementation of IPatientManager

        public List<PATIENT> GetPatients()
        {
            using (var context = new ChildHouseDBEntities())
            {
                foreach (PATIENT patient in context.PATIENTs)
                {
                    context.LoadProperty(patient, "TREATMENT_COURSE");
                }

                List<PATIENT> patients = context.CreateObjectSet<PATIENT>().ToList();
                return patients;
            }
        }

        public PATIENT GetPatientById(int id)
        {
            using (var context = new ChildHouseDBEntities())
            {
                return context.PATIENTs.FirstOrDefault(p => p.Id == id);
            }
        }

        public List<PATIENT> GetUsersMinMax(int startIndex, int maxCount)
        {
            using (var conext = new ChildHouseDBEntities())
            {
                return conext.PATIENTs.Skip(startIndex).Take(maxCount).ToList();
            }
        }

        public int GetPatientCount()
        {
            using (var context = new ChildHouseDBEntities())
            {
                return context.PATIENTs.Count();
            }
        }

        public void DeletePatient(PATIENT deletedPatient)
        {
            using (var context = new ChildHouseDBEntities())
            {
                context.DeleteObject(deletedPatient);
                context.SaveChanges();
            }
        }

        public PATIENT EditPatient(PATIENT newPatient)
        {
            using (var context = new ChildHouseDBEntities())
            {
                PATIENT patientOld = new PATIENT();
                patientOld = context.PATIENTs.Single(r => r.Id == newPatient.Id);
                //patientOld = _objectMapper.Map<PATIENT,PATIENT>(newPatient);
                patientOld.ADRESS = newPatient.ADRESS;
                patientOld.AMBULATORY = newPatient.AMBULATORY;
                patientOld.BIRTHDATE = newPatient.BIRTHDATE;
                patientOld.CARD = newPatient.CARD;
                patientOld.Diagnosis = newPatient.Diagnosis;
                patientOld.ENTER_DATE = newPatient.ENTER_DATE;
                patientOld.Examinations = newPatient.Examinations;
                patientOld.FULLNAME = newPatient.FULLNAME;
                patientOld.GENDER = newPatient.GENDER;
                patientOld.Healings = newPatient.Healings;
                patientOld.ID_TREATMENT_COURSE = newPatient.ID_TREATMENT_COURSE;
                patientOld.INVAL = newPatient.INVAL;
                patientOld.IS_CHORNOBUL = newPatient.IS_CHORNOBUL;
                patientOld.IS_CITIZEN = newPatient.IS_CITIZEN;
                patientOld.IS_SANATORY = newPatient.IS_SANATORY;
                patientOld.PHONE = newPatient.PHONE;
                patientOld.RELEASE_DATE = newPatient.RELEASE_DATE;
                patientOld.TREATMENT_COURSE = newPatient.TREATMENT_COURSE;
                //context.AttachTo(newPatient.GetType().Name, newPatient);
                //context.ObjectStateManager.ChangeObjectState(newPatient, EntityState.Modified);
                context.SaveChanges();
                return context.PATIENTs.FirstOrDefault(p => p.Id == newPatient.Id);
            }
        }

        public PATIENT InsertPatient(PATIENT newPatient)
        {
            using (var context = new ChildHouseDBEntities())
            {
                context.PATIENTs.AddObject(newPatient);
                context.SaveChanges();
                return context.PATIENTs.FirstOrDefault(p => p.Id == newPatient.Id);
            }
        }

        #endregion
    }
}
